package com.example.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.Result;
import com.example.demo.entity.Sign;
import com.example.demo.entity.User;
import com.example.demo.mapper.SignMapper;
import com.example.demo.service.ISignService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;


/**
 * @descreption:
 * @author: xhr
 * @date: 2022/9/03
 */
@RestController
@RequestMapping("/sign")
public class SignController {
    @Resource
    SignMapper signMapper;
    @Resource
    private ISignService signService;
    //增
    @PostMapping
    public Result<?> save(@RequestBody Sign sign) {

        Sign res=signMapper.selectOne(new QueryWrapper<Sign>().eq("user_id", sign.getUserId()).eq("exam_id",sign.getExamId()));
        if (res == null) {
            signMapper.insert(sign);
            return Result.success();
        }else {
            return Result.error("-1","已报名！待审核通过后，再刷新。");
        }

    }
    // 改
    @PutMapping
    public Result<?> update(@RequestBody Sign sign) {
        signMapper.updateById(sign);
        return Result.success();
    }
    // 删
    @DeleteMapping("/{id}")
    public Result<?> update(@PathVariable Long id) {
        signMapper.deleteById(id);
        return Result.success();
    }

    //全(页)查
    @GetMapping("/page")
    public Result<?> findPage(@RequestParam(defaultValue = "10") Integer pagesize,
                              @RequestParam(defaultValue = "1") Integer pagenum) {
        LambdaQueryWrapper<Sign> wrapper = Wrappers.<Sign>lambdaQuery();

        return Result.success(signMapper.selectPage(new Page<>(pagenum, pagesize), wrapper));
    }

}
